import Crypt from "../../../utils/Crypt";
import UserModel from "../../../models/User";

const userModel = new UserModel()

Page({
  data: {
    type: 0,
    page: 0,
    pageSize: 10,
    list: [],
    busy: false,
    set: {},
    bank: {},
    submitData: {}
  },
  onLoad: function (options) {
    // this.setData({ id: options.id })
    this._getList()
  },

  onShow() {
    this.getSet()
  },

  checkSetPassword() {
    userModel.checkSetPassword().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      if (!data.result) {
        wx.showToast({
          title: '您还没有设置支付密码',
          icon: 'none',
          duration: 1000
        })

        // this.setData({ type: 0 })

        setTimeout(() => {
          wx.navigateTo({
            url: '/sub_service/pages/pay-password/index'
          })
        }, 800)
      }
    })
  },

  getSet() {
    userModel.cashFetch().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      this.setData({
        set: data
      })
      if (data.bank) {
        this.setData({
          bank: data.bank,
          submitData: { ...data.bank }
        })
      }
      this.setData({
        submitData: {
          ...this.data.submitData,
          amount: data.set.mini
        }
      })
    })
  },

  onBankName(e) {
    const bank_name = e.detail.value
    this.setData({
      submitData: {
        ...this.data.submitData,
        bank_name
      }
    })
  },
  onBankNumber(e) {
    const bank_number = e.detail.value
    this.setData({
      submitData: {
        ...this.data.submitData,
        bank_number
      }
    })
  },
  onNickname(e) {
    const nickname = e.detail.value
    this.setData({
      submitData: {
        ...this.data.submitData,
        nickname
      }
    })
  },
  onMobile(e) {
    const mobile = e.detail.value
    this.setData({
      submitData: {
        ...this.data.submitData,
        mobile
      }
    })
  },
  onAmount(e) {
    const amount = e.detail.value
    this.setData({
      submitData: {
        ...this.data.submitData,
        amount
      }
    })
  },

  cashSubmit() {
    const data = this.data.submitData
    if (Object.keys(data).length < 5) {
      wx.showToast({ title: '请填完整', icon: 'error' })
    } else if (isNaN(data.amount) || +data.amount <= 0) {
      wx.showToast({ title: '提现金额错误', icon: 'error' })
    } else if (+data.amount < +this.data.set.set.mini) {
      wx.showModal({ title: '提现金额错误', content: '最小为:' + this.data.set.set.mini })
    } else if (+data.amount > +this.data.set.balance) {
      wx.showModal({ title: '提现金额错误', content: '可提金额为:' + this.data.set.balance })
    } else {
      this.setData({
        showPayPassword: true
      })
    }
  },

  pay_password(e) {
    const value = e.detail.value
    this.setData({
      password: value
    })
  },

  close() {
    this.setData({
      showPayPassword: false
    })
  },

  submitPayPassword() {
    const password = this.data.password
    if (!password || password.length < 1) {
      wx.showToast({ title: '请输入支付密码', icon: 'error' })
    } else {
      userModel.verifyPayPassword({ password }).then(response => {
        const { data } = response
        const is = data.result
        if (is) {
          // submit cash to db
          userModel.cashSave({
            bank: { ...this.data.submitData },
            amount: this.data.submitData.amount
          }).then(() => {
            this.setData({
              showPayPassword: false
            })
            wx.showToast({
              title: '提现成功,等待审核',
              success: () => {
                setTimeout(() => {
                  this.setData({
                    type: 2,
                    page: 0,
                    pageSize: 10,
                    list: [],
                    busy: false,
                  })
                  this._getList()
                }, 600)
              }
            })
          })
        } else {
          wx.showToast({ title: '支付密码错误', icon: 'error' })
        }
      })
    }
  },

  onReachBottom() {
    this._getList(true)
  },

  _getList(append = false) {
    if (this.data.busy === false) {
      this.setData({
        busy: true,
        page: this.data.page + 1
      })
      userModel.cashList({
        ...this.data,
      }).then(response => {
        let { data } = response
        data = Crypt.parse(data)
        if (data.list.length === 0) {
          this.setData({ busy: true })
        } else {
          this.setData({ busy: false })
          let list = []
          if (append) {
            list = this.data.list.concat(data.list)
          } else {
            list = data.list
          }
          // 状态
          list.forEach(x => {
            x.statusText = this.statusText(x.status)
          })
          this.setData({ list })
        }
      })
    }
  },

  statusText(v) {
    let text = ''
    if (+v === 0) {
      text = '审核中'
    } else if (+v === 1) {
      text = '已到帐'
    } else if (+v === -1) {
      text = '已驳回'
    }
    return text
  },

  handleClick(e) {
    const type = e.currentTarget.dataset.type
    if (type != this.data.type) {

      if (type == 4) {
        // 查看设置提现密码没有
        this.checkSetPassword()
      }

      this.setData({
        type,
        page: 0,
        pageSize: 10,
        list: [],
        busy: false,
      })
      this._getList()
    }
  }

});
